package com.sunnada.edu.business.attendance.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.attendance.bean.Attendance;
import com.sunnada.edu.business.attendance.service.AttendanceMethods;
import com.sunnada.edu.business.attendance.service.AttendanceService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 考勤管理层
 * 
 * @author zhenglx
 */
@Controller
@RequestMapping("/attendanceController")
public class AttendanceController extends BasicController {
	@Resource
	AttendanceService attendanceService;

	/** 考勤首页 */
	@RequestMapping("/attendance")
	public String attendance(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		HashMap<String, String> params = new HashMap<String, String>();
		List<?> l = attendanceService.getprojects(params);
		request.setAttribute("projectList", l);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		//AttendanceMethods.AttendanceStatistics(null);
		return "business/attendance/attendance";
	}

	/** 考勤查询 */
	@RequestMapping("/attendanceSearch")
	public void attendanceSearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		Page page = new Page();
		page.setRowCount(attendanceService.getAttendanceCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = attendanceService.findAttendance(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 考勤添加页面 */
	@RequestMapping("/attendanceAdd")
	public String attendanceAdd(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		List<?> l = attendanceService.getprojects(params);
		request.setAttribute("projectList", l);
		return "business/attendance/attendance_add";
	}

	/** 考勤修改页面 */
	@RequestMapping("/attendanceUpdate")
	public String attendanceUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		Map attend = attendanceService.attendanceByRecId(params);
		request.setAttribute("attend", attend);
		String wokIds = attendanceService.attenworkerById(params);
		request.setAttribute("wokIds", wokIds);
		List<?> pr = attendanceService.getprojects(params);
		request.setAttribute("projectList", pr);
		return "business/attendance/attendance_update";
	}
	
	
	/** 考勤修改页面 */
	@RequestMapping("/attendanceDetails")
	public String attendanceDetails(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		Map attend = attendanceService.attendanceByRecId(params);
		request.setAttribute("attend", attend);
		String wokIds = attendanceService.attenworkerById(params);
		request.setAttribute("wokIds", wokIds);
		List<?> pr = attendanceService.getprojects(params);
		request.setAttribute("projectList", pr);
		return "business/attendance/attendance_details";
	}

	/** 考勤保存功能 */
	@RequestMapping("/attendanceSave")
	public void attendanceSave(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		String wkIds = request.getParameter("wkIds");
		params.put("wkIds", wkIds);
		// 验证用户账号是否重复
		String recId = params.get("recId");
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("orgName", params.get("orgName"));
		Map attend = attendanceService.attendanceByRecId(param);
		msg = "1";
		if(Utils.isNotNull(attend)){
			if(Utils.isNotNull(recId)){
				HashMap<String, String> param1 = new HashMap<String, String>();
				param1.put("recId", recId);
				Map attend1 = attendanceService.attendanceByRecId(param1);;
				if(attend1.get("ORG_NAME").equals(attend.get("ORG_NAME"))){
					msg = "1";
				}else{
					msg = "2";
				}
			}else{
				msg = "2";
			}
		}
		if(msg.equals("1")){
			if (Utils.isNotNull(recId)) {
				msg = attendanceService.attendanceUpdate(params);
			} else {
				recId = UUID.randomUUID().toString();
				params.put("recId", recId);
				msg = attendanceService.attendanceAdd(params);
			}
		}
		printInfo(response, msg);
	}

	/** 考勤删除功能 */
	@RequestMapping("/attendanceDelete")
	public void attendanceDelete(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = attendanceService.attendanceDelete(params);
		printInfo(response, msg);
	}

	@RequestMapping("/attenWorker")
	public String attenWorker(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		String wokIds = attendanceService.findAttenworker(params);
		request.setAttribute("wokIds", wokIds);
		request.setAttribute("wkId", request.getParameter("wkId"));
		List<?> g = attendanceService.getWorkerGroup(params);
		request.setAttribute("groupList", g);
		return "business/attendance/attenWorker";
	}
	
	
	@RequestMapping("/getAttenWorkerSearch")
	public void getAttenWorkerSearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		Page page = new Page();
		String wkIds = request.getParameter("wokIds");
		String wkId = request.getParameter("wkId");
		String groupId = request.getParameter("groupId");
		String userName = request.getParameter("userName");
		String condition = "";
		if(Utils.isNotNull(wkIds)){
			condition += "AND T.JOBNO NOT IN("+Utils.getSqlSplitStr(wkIds)+")";
		}
		if(Utils.isNotNull(wkId)){
			condition += "AND T.JOBNO NOT IN("+Utils.getSqlSplitStr(wkId)+")";
		}
		
		if(Utils.isNotNull(groupId)){
			condition += "AND (SELECT g.NAME FROM T_WORKER_GROUP g where g.REC_ID=T.GROUPID and g.rec_status!='2') like '%"+groupId+"%'";
		}
		if(Utils.isNotNull(userName)){
			condition += "AND T.NAME like '%"+userName+"%'";
		}
		params.put("condition", condition);
		params.put("SUNNADA_PAGE_SIZE", request.getParameter("SUNNADA_PAGE_SIZE"));
		page.setRowCount(attendanceService.getWorkerCots(params));
		page.setPageMap(request, page, params);
		String jobnos = attendanceService.getWorkers(params);
		List<?> userList = attendanceService.findWorkers(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		json.put("jobnos", jobnos);
		printInfo(response, json);
	}
	
	@RequestMapping("/attenWorkerSearch")
	public void attenWorkerSearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		Page page = new Page();
		String wkIds = request.getParameter("wkIds");
		String condition = "";
		if(Utils.isNotNull(wkIds)){
			condition = "AND T.JOBNO IN("+Utils.getSqlSplitStr(wkIds)+")";
		}
		params.put("condition", condition);
		params.put("SUNNADA_PAGE_SIZE", request.getParameter("SUNNADA_PAGE_SIZE"));
		page.setRowCount(attendanceService.getWorkerCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = attendanceService.findWorkers(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}
	
	
}
